const request = require('../../utils/request')
Component({

    /**
     * 组件的属性列表
     */
    properties: {
        itemData: {
            // 类型
            type: Object,
            // 默认值
            value: {}
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        hasClickLike: false
    },

    /**
     * 组件的方法列表
     */
    methods: {
        toDetail() {
            const id = this.properties.itemData.id
            wx.navigateTo({
                url: '/pages/detail/detail?id=' + id,
            })
        },
        handleLike(event) {
            const that = this
            const token = wx.getStorageSync('token')
            if (!token) {
                wx.showToast({
                    icon: "none",
                    title: '登入后才能点赞',
                })
                return
            }
            if (this.data.itemData.isLike || this.data.itemData.userScript != null) {
                return false
            }
            this.setData({
                "itemData.isLike": true
            })
            const userinfo = JSON.parse(wx.getStorageSync('userinfo'))
            request({
                url: `/script/like/${this.data.itemData.id}`,
                method: "put",
                data: {
                    scriptId: this.data.itemData.id,
                    userId: userinfo.id,
                    isLike: true
                },
                header: {
                    "X-Token": token
                },
                success(data) {
                    that.setData({
                        "itemData.likeNum": that.data.itemData.likeNum + 1,
                        hasClickLike: true
                    })
                },
                fail(err) {
                    console.log('请求失败：', err);
                }
            })
        }
    }
})